﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using Me.Amon.CRM.C;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.CRM;
using Me.Amon.Da.Db.POS;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class Terminals : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_06)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var corp = Request["corp"];
            if (!CharUtil.IsValidateCode(corp))
            {
                corp = "0";
            }
            var dba = new RDBEngine();
            ListCompany(dba, corp);
        }

        protected void LbCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListTerminal(new RDBEngine(), 0);
        }

        #region 导航
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnAppendTerminal_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/UpdateTerminal.aspx?corp=" + LbCompany.SelectedValue);
        }
        #endregion

        private void ListCompany(RDBEngine dba, string corp)
        {

            dba.AddTable(DCompany.TABLE);
            dba.AddColumn(DCompany.NAME);
            dba.AddColumn(DCompany.CODE);
            dba.AddColumn(DCompany.ADDRESS);
            //if (corp != "0")
            //{
            //    dba.AddWhere(DCompany.CODE, corp);
            //}
            dba.AddWhere(DCompany.STATUS, CCompany.STATUE_ENABLED);

            var dt = dba.ExecuteSelect();
            LbCompany.DataTextField = DCompany.NAME;
            LbCompany.DataValueField = DCompany.CODE;
            LbCompany.DataSource = dt;
            LbCompany.DataBind();
            if (dt.Rows.Count > 0)
            {
                if (corp == "0")
                {
                    LbCompany.SelectedIndex = 0;//根据corp确定SelectedIndex
                }
                LbCompany.SelectedValue = corp;
                ListTerminal(new RDBEngine(), 0);
            }
        }

        private void ListTerminal(RDBEngine dba, int page)
        {
            dba.AddTable(DTerminal.TABLE, "a");
            dba.AddTable(DCompany.TABLE, "b");
            dba.AddTable(DShop.TABLE, "c");
            dba.AddColumn("b." + DCompany.NAME);
            dba.AddColumn("a." + DTerminal.ID);
            dba.AddColumn("a." + DTerminal.CORP_ID);
            dba.AddColumn("a." + DTerminal.CODE);
            dba.AddColumn("a." + DTerminal.NAME);
            dba.AddColumn("a." + DTerminal.SHOP_ID);
            dba.AddColumn("c." + DShop.NAME,"shop_name");
            dba.AddColumn("a." + DTerminal.ADDR);
            dba.AddColumn("a." + DTerminal.STATUS);
            dba.AddWhere("a." + DTerminal.CORP_ID, LbCompany.SelectedValue);
            dba.AddWhere("b." + DCompany.CODE, LbCompany.SelectedValue);
            dba.AddWhere("a." + DTerminal.CORP_ID, "c." + DShop.CORP_ID, false, false);
            dba.AddWhere("a." + DTerminal.SHOP_ID, "c." + DShop.CODE,false,false);

            var table = dba.ExecuteSelect();
            int cnt = table.Rows.Count;

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpShops.DataSource = pds;
            RpShops.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            ListTerminal(new RDBEngine(), 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            ListTerminal(new RDBEngine(), max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            ListTerminal(new RDBEngine(), max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            ListTerminal(new RDBEngine(), max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            ListTerminal(new RDBEngine(), max);
        }
        #endregion
    }
}